﻿/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using Shuriken.Base;

namespace Shuriken.Core.Store
{
    public class QNStoreItem : IStoreItem, ISubject, ISearchable 
    {
        protected static string DEFAULT_IMAGE = string.Empty;

        private string m_Icon; 
        private double m_Rank;
        private Shuriken.Core.Ranking.MutableIndexSet m_HitMask;
        private string m_SearchTerm; 
        private IStore m_Store;

        private ISubject m_Subject;
        static QNStoreItem()
        {
            DEFAULT_IMAGE = ImageStore.GetDefaultSubjectImage();
        }

        public QNStoreItem(ISubject subject)
        {
            System.Diagnostics.Debug.Assert(subject != null, "QNStoreItem received a null subject!");
            m_Subject = subject;
            m_Icon = m_Subject.Icon; 
        }

        /// <summary>
        /// Gets inner subject stored in this instance
        /// </summary>
        public ISubject InnerSubject
        {
            get
            {
                return m_Subject; 
            }
        }

        #region IStoreItem Members

        /// <summary>
        /// Store where the item exists
        /// </summary>
        public IStore Store
        {
            get
            {
                return m_Store;
            }
            set
            {
                m_Store = value;
            }
        }

        public void RemoveItemFromStore()
        {
            Store.RemoveItem(this);
            m_Store = null;
        }

        public void RegisterActions()
        {
            List<IAction> actions = m_Subject.GetListOfActions();

            foreach (IAction a in actions)
            {
                QNAction action = new QNAction(a);
                ActionManager.GetDefaultInstance().RegisterAction(m_Subject.GetType(), action); 
            }            
        }

        /// <summary>
        /// Register parent's action for this type. 
        /// </summary>
        /// <remarks>
        /// Register parent's action for this type. These actions are coming 
        /// from the parent classes. 
        /// </remarks>
        public virtual void RegisterParentActions()
        {
            List<IAction> actions = new List<IAction>();
            Type regType = this.GetType();
            Type baseType = regType.BaseType;

            // BaseFolderItem is not going to have any actions 
            // associated to it, so let's skip this type
            if (!IsInterfaceImplemented(baseType, typeof(ISubject)))
            {
                return;
            }

            ActionManager.GetDefaultInstance().GetListOfActions(baseType, actions);
            System.Diagnostics.Debug.Assert(actions.Count != 0, "No Action was returned for parent type!");
            ActionManager.GetDefaultInstance().RegisterActions(regType, actions);
        }

        private bool IsInterfaceImplemented(Type baseType, Type iface)
        {
            Type[] inta = baseType.GetInterfaces();

            foreach (Type t in inta)
            {
                if (t.Equals(iface))
                {
                    return true;
                }
            }

            return false; 
        }

        #endregion

        #region ISubject Members

        public void ExecuteDefaultAction()
        {
            IAction action = ActionManager.GetDefaultInstance().GetDefaultAction(m_Subject.GetType());
            action.Execute(this);
        }

        public List<IAction> GetListOfActions()
        {
            List<IAction> listOfActions = new List<IAction>();
            ActionManager.GetDefaultInstance().GetListOfActions(m_Subject, listOfActions);
            return listOfActions;
        }

        #endregion

        #region IObject Members

        public string Name
        {
            get { return m_Subject.Name; }
        }

        public string Description
        {
            get { if(m_Store != null) { return m_Store.StoreName + "\\" + m_Subject.Name; } else { return string.Empty;} }
        }

        public string Icon
        {
            get
            {
                return m_Icon;
            }
            set
            {
                m_Icon = value;
            }
        }

        #endregion

        #region ISearchable Members

        /// <summary>
        /// Rank for the item. 
        /// </summary>
        public double Rank
        {
            get
            {
                return m_Rank;
            }

            set
            {
                m_Rank = value;
            }
        }

        /// <summary>
        /// HitMask for this item
        /// </summary>
        public Shuriken.Core.Ranking.MutableIndexSet HitMask
        {
            get
            {
                return m_HitMask;
            }
            set
            {
                m_HitMask = value;
            }
        }

        /// <summary>
        /// Last search term used to select this item 
        /// </summary>
        public string SearchTerm
        {
            get
            {
                return m_SearchTerm; 
            }
            set
            {
                m_SearchTerm = value; 
            }
        }

        /// <summary>
        /// Unique identifier 
        /// </summary>
        public string UniqueIdentifier
        {
            get { 
                return string.Format("{0}.{1}", m_Subject.GetType(), m_Subject.Name);
            }
        }

        #endregion

        /// <summary>
        /// Stores an image in Image Store
        /// </summary>
        /// <param name="image">Image to store</param>
        /// <returns>Index of image</returns>
        protected static string StoreIcon(System.Drawing.Icon icon)
        {
            return ImageStore.AddResourceImage(icon);
        }

        public override string ToString()
        {
            if (Store != null)
            {
                return Rank.ToString("0.000") + "." + Store.StoreName + "." + Name;
            }
            else
            {
                return Rank.ToString("0.000") + ".VoidStore." + Name;
            }
        }
    }
}
